import random
import uuid
from Config.RunConfig import RunConfig, Options


class CommonOperations:
    def __init__(self):
        self._operationsList = [
            self._getCloudInfo,
            self._getServerInfo,
            self._getCustomColumnInfoById,
            self._getDownloadLinkVerb,
            self._enumerateRegions,
            self._enumeratePermissions,
            self._enumerateStatisticsHistoryAggregateDescriptors,
            self._enumerateAggregatedStatisticsHistory,
            self._enumerateAllowedUpdatePackageVersions,
            self._enumerateAuditActions,
            self._enumerateInvoices
        ]
        if RunConfig().get(Options.StressTest.stressAllPartners) == 'True':
            self._operationsList.append(self._login)
            self._operationsList.append(self._addRegion)
            self._operationsList.append(self._addStatisticsHistoryAggregate)

        self._baseGrowthRate = 1

    def _getRandomOperation(self):
        return random.choice(self._operationsList)

    def _addRegion(self, manserv, partnerId):
        manserv.AddRegion(id = random.randint(0, 1000), name = uuid.uuid4())

    def _addStatisticsHistoryAggregate(self, manserv, partnerId):
        manserv.AddStatisticsHistoryAggregate(id = partnerId, name = uuid.uuid4(), unit = random.randint(0, 1000),
                                              expression =  random.randint(0, 1000))

    def _login(self, manserv, partnerId):
        manserv.Login(partnerName = 'IASO', userName = 'root', password = 'IASO1234')

    def _loginStressTestUser(self, manserv, partnerId):
        manserv.Login(partnerName = 'StressTestPartner', userName = 'stresstestuser', password = 'stresstestuser')

    def _getCloudInfo(self, manserv, partnerId):
        manserv.GetCloudInfo()

    def _getServerInfo(self, manserv, partnerId):
        manserv.GetServerInfo()

    def _getCustomColumnInfoById(self, manserv, partnerId):
        manserv.GetCustomColumnInfoById(customColumnId = random.randint(1, 10))

    def _getDownloadLinkVerb(self, manserv, partnerId):
        manserv.GetDownloadLinkVerb(accountId = random.randint(1, 100), osName = 'TestOs', osType = 'osType',
                                    fileExtension = 'txt')

    def _enumerateRegions(self, manserv, partnerId):
        manserv.EnumerateRegions()

    def _enumeratePermissions(self, manserv, partnerId):
        manserv.EnumeratePermissions()

    def _enumerateStatisticsHistoryAggregateDescriptors(self, manserv, partnerId):
        manserv.EnumerateStatisticsHistoryAggregateDescriptors()

    def _enumerateAggregatedStatisticsHistory(self, manserv, partnerId):
        param = {
            'partnerId': partnerId,
            'from': '2010-04-17T16:50:45',
            'to': '2034-04-17T16:50:45',
            'aggregates': random.randint(0, 10)
        }
        manserv.EnumerateAggregatedStatisticsHistory(**param)

    def _enumerateAllowedUpdatePackageVersions(self, manserv, partnerId):
        manserv.EnumerateAllowedUpdatePackageVersions(partnerId = partnerId)

    def _enumerateAuditActions(self, manserv, partnerId):
        param = {
            'partnerId': partnerId,
            'from': '2010-04-17T16:50:45',
            'to': '2034-04-17T16:50:45',
            'userId': random.randint(0, 3)
        }
        manserv.EnumerateAuditActions(**param)

    def _enumerateInvoices(self, manserv, partnerId):
        manserv.EnumerateInvoices(partnerId = partnerId, year = random.randint(2010, 2020),
                                  month = random.randint(1, 12),
                                  depth = random.randint(1, 10), format = random.choice(['pdf', 'html']),
                                  granularity = random.choice(['SingleInvoicePerPartner', 'AllInvoicesTogether']))
